package app.fugema.shop.main.evaluate.view;

import java.util.ArrayList;
import java.util.List;

import com.base.common.app.LoginUtils;
import com.base.common.databinding.BaseTitleToolbarBinding;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import app.fugema.shop.R;
import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.databinding.ActivityProductQuestionListBinding;
import app.fugema.shop.main.evaluate.adapter.GoodsQuestionAdapter;
import app.fugema.shop.main.evaluate.model.QusetionHeadBean;
import app.fugema.shop.main.evaluate.model.QusetionResult;
import app.fugema.shop.main.evaluate.vm.EvaluateVM;
import app.fugema.shop.main.goods.model.GoodsDetailResult;
import app.fugema.shop.main.login.model.MUserInfo;
import app.fugema.shop.util.SimpleTextWatcher;

public class GoodsQuestionActivity extends BaseTitleActivity<ActivityProductQuestionListBinding, EvaluateVM> implements OnGetDataListener {
    //商品评论列表

    public static void start(Context context, GoodsDetailResult.ProductBean goodsBean)
    {
        Intent starter = new Intent(context, GoodsQuestionActivity.class);
        starter.putExtra("product", JacksonUtils.transBean2Json( goodsBean ));
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_question_list);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    public void initToolBar(BaseTitleToolbarBinding mTitleBinding)
    {
        super.initToolBar(mTitleBinding);
        //mTitleBinding.clToolbar.setBackgroundColor(ColorUtils.getColor(R.color.C_FFFFFF));
    }

    private GoodsQuestionAdapter adapter;
    private GoodsDetailResult.ProductBean productBean;
    private MUserInfo mUser;
    private QusetionHeadBean qusetHead;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("问问大家",R.color.white);
        adapter = new GoodsQuestionAdapter();
        binding.rvRecyclerView.setAdapter(adapter);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

        // 问题
        binding.etQuestion.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(String content)
            {

            }
        });
        //提交
        binding.btnApplyAsk.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {
                String title = binding.etQuestion.getText().toString();

                 if( LoginUtils.isLogin() && UIUtils.isNotEmpty( title ) )
                 {
                     viewModel.applyQuestion(productBean.getProductId(),mUser.getUserId(),title)
                              .observe(GoodsQuestionActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                              {
                                  @Override
                                  protected void onSuccess(LiveDataWrapper<BaseApiResult> data) {
                                      //{"success":true, "msg":"添加成功！"}
                                      if( data.data.isSuccess() )
                                      {
                                          UIUtils.showToastSafes( data.data.getMsg() );
                                          binding.etQuestion.setText("");
                                          // 刷新 问题
                                          onGetData(1);
                                      }
                                  }
                              });
                 }
            }
        });
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        String prodStr = intent.getStringExtra("product");
        productBean = JacksonUtils.getJsonBean(prodStr,GoodsDetailResult.ProductBean.class);
        if(LoginUtils.isLogin())
        {
            mUser = PublicGlobal.getUserInfo();
        }
        if( UIUtils.isNotEmpty( productBean ) )
        {
            qusetHead = new QusetionHeadBean();
            qusetHead.setItemType(BaseItemMultiType.TYPE_HEAD);
            qusetHead.setProductId( productBean.getProductId() );
            qusetHead.setProductName( productBean.getProductName() );
            if( UIUtils.isNotEmpty( productBean.getImagePath() ) && !productBean.getImagePath().isEmpty() )
            {
                qusetHead.setProductImg( productBean.getImagePath().get(0) );
            }
            qusetHead.setQuestionCount(0);
            adapter.addHeaderView(qusetHead, 0);
        }
        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {

        viewModel.getQuestionByProductId(productBean.getProductId(), pn)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<QusetionResult>>(viewUtils.getStateLayout())
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<QusetionResult> data)
                    {
                        if( UIUtils.isNotEmpty( data.data.getData() ) )
                        {
                            qusetHead.setQuestionCount(  data.data.getData().size() );
                            adapter.notifyItemChanged( adapter.getPosition( qusetHead ) );
                        }
                        viewUtils.setDataListRefreshLayout(adapter, pn, 10, data.data.getData());
                    }

                    @Override
                    public boolean isRefreshLayoutData()
                    {
                        return true;
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<QusetionResult> data)
                    {
                        return  (pn == 1 && UIUtils.isEmpty(data.data.getData()));
                    }
                 });
    }

}

